package com.sskj.cndragon.presenter;

import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.cndragon.bean.RecordListBean;
import com.sskj.cndragon.bean.ShaiTuListBean;
import com.sskj.cndragon.http.HttpConfig;
import com.sskj.cndragon.ui.activity.GetQuanActivity;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.SPConfig;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.http.JsonConverter;

import java.io.File;
import java.util.List;

import io.reactivex.Flowable;


public class GetQuanActivityPresenter extends BasePresenter<GetQuanActivity> {

    public void uploadFile(File file) {
        httpService.uploadFile(file)
                .tag(this)
                .execute(new JsonCallBack<HttpData<String>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<String>> response) {
                        HttpData body = response.body();
                        if (body.getStatus() == HttpConfig.OK) {
                            mView.changeHead((String) body.getData());
                        } else {
                            ToastUtil.showShort(body.getMsg());
                        }
                    }
                });
    }

    public void shaiTu(String userID, String imageUrl) {
        httpService.shaiTu(userID, imageUrl).tag(this)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData body = response.body();
                        ToastUtil.showShort(body.getMsg());
                        if (body.getStatus() == HttpConfig.OK) {
                            mView.shaituOK();
                        } else {
                            mView.shaituFail();
                        }
                    }
                });
    }

    public Flowable<List<ShaiTuListBean.ShaiTuBean>> getShaiTuRecord(int page) {
        return httpService.getShaiTuRecord(SPUtil.get(SPConfig.ID, ""), page)
                .tag(this)
                .converter(new JsonConverter<HttpData<ShaiTuListBean>>() {
                })
                .adapt(new FlowableBody<>())
                .map(listHttpData -> listHttpData.getData().getList());
    }
}
